const lodash = require('lodash')
import { DeepCopy } from "@/utils/Utils"
import Base from "../entities/Base"

const JobFilterBtnMixin = {
    data() {
        return {
            page: 1,
            status: -1,
            dialogFormVisible: false,
            headerConfig: {
                total: 1,
                showPage: true,
                pageSize: Base.pageSize,
                specific: {
                    is: false,
                    ids: [],
                },
                whereDict: {},
                statusBtns: [
                    { status: 0, name: "待采集", key: "status" },
                    { status: 1, name: "已采集", key: "status" },
                    { status: 2, name: "已生成", key: "status" },
                    { status: 3, name: "已发布", key: "status" }
                ]
            }
        }
    },
    methods: {
        getTargets(page) {

        },
        handleSpecific(args) {
            let { data, refs, className } = args
            let ref = refs[data].$el
            if (ref.classList.contains(className)) {
                this.specific.is = false
                this.specific.ids = []
                this.clearTableSelection()
                ref.classList.remove(className)
            } else {
                this.specific.is = true
                ref.classList.add(className)
            }

            console.log("specific", this.specific)
        },
        handleSelectionChange(vals) {
            this.specific.is = true
            this.$refs.btnGroup.$refs["特定"].$el.classList.add("clicked")
            for (let val of vals) {
                if (!this.specific.ids.includes(val.id)) {
                    this.specific.ids.push(val.id)
                }
            }
        },
        handleCurrentPage(page) {
            this.page = page
            this.getTargets(page)
        },
        handleStatusFilter(row) {
            this.status = row.status
            this.getTargets(this.page)
        },
        clearTableSelection() {
        },
        beforeStatusFilter(args) {
            let { data, refs, className } = args
            let ref = refs[data.name][0].$el
            let groups = lodash.groupBy(this.headerConfig.statusBtns, "key")
            let btns = groups[data.key]
            if (this.whereDict[data.key].indexOf(-1) != -1) {
                ref.classList.remove(className)
                //当前按钮是全部
                let statuss = []
                for (let btn of btns) {
                    if (btn.name != data.name) {
                        statuss.push(btn.status)
                    }
                }
                this.whereDict[data.key] = statuss
            } else if (this.whereDict[data.key].indexOf(data.status) != -1) {
                //当前按钮是Highlight
                let index = this.whereDict[data.key].indexOf(data.status)
                if (index != -1) {
                    this.whereDict[data.key].splice(index, 1)
                }
                ref.classList.remove(className)
            } else {
                ref.classList.add(className)
                this.whereDict[data.key].push(data.status)
            }
        },
        handleInitialWhere() {
            let groups = lodash.groupBy(this.headerConfig.statusBtns, "key")
            let keys = Object.keys(groups)
            let dict = DeepCopy(this.whereDict)
            for (let key of keys) {
                if (!lodash.isEmpty(dict[key])) {
                    continue
                }
                let statuss = []
                for (let btn of groups[key]) {
                    statuss.push(btn.status)
                }
                this.whereDict[key] = statuss
            }
            return this.whereDict
        },
        handleHeaderClick(args) {
            if (!lodash.isUndefined(args.value)) {
                this[args.name](args.value)
            } else {
                this[args.name]()
            }
        },
    }
}

export { JobFilterBtnMixin }